package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqDepartment;
import cloud.maque.admin.domain.vo.MqDepartmentVo;
import cloud.maque.admin.mapper.MqDepartmentMapper;
import cloud.maque.admin.service.MqDepartmentService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhengkai.blog.csdn.net
 * @description 部门表
 * @date 2023-03-17
 */
@Service
public class MqDepartmentServiceImpl extends ServiceImpl<MqDepartmentMapper, MqDepartment> implements MqDepartmentService {


    @Override
    public MqDepartment saveDept(MqDepartment department) {
        Assert.hasText(department.getDeptName(), "部门名称不能为空");
        this.saveOrUpdate(department);
        return department;
    }

    @Override
    public boolean update(MqDepartment department) {
        Assert.hasText(department.getDeptId(), "id must not null");
        return this.updateById(department);
    }

    /**
     * 删除部门，如果有下级部门，则不能删除
     * 如果此部门，还有下级部门，则不能删除
     *
     * @param deptId
     * @return
     */
    @Override
    @Transactional
    public boolean delete(String deptId) {

        if (getByParentId(deptId) > 0) {
            throw new RuntimeException("此部门还有下级部门，不能删除");
        }

        //todo: 还有用户

        return this.removeById(deptId);
    }

    @Override
    public Integer getByParentId(String deptId) {

        QueryWrapper<MqDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqDepartment::getParentId, deptId);
        return this.count(queryWrapper);
    }

    @Override
    public PageInfo<MqDepartment> page(PaginateDataResquest<MqDepartment> pageReq) {
        PageHelper.startPage(pageReq.getPageIndex(), pageReq.getPageSize());
        QueryWrapper<MqDepartment> wrapper = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getDeptName())) {
            wrapper.lambda().like(MqDepartment::getDeptName, pageReq.getData().getDeptName());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getXzqDm())) {
            wrapper.lambda().like(MqDepartment::getXzqDm, pageReq.getData().getXzqDm());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getDeptCode())) {
            wrapper.lambda().like(MqDepartment::getDeptCode, pageReq.getData().getDeptCode());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getTenantId())) {
            wrapper.lambda().eq(MqDepartment::getTenantId, pageReq.getData().getTenantId());
        }

        wrapper.lambda().orderByAsc(MqDepartment::getDeptSort);
        wrapper.lambda().orderByAsc(MqDepartment::getDeptId);
        List<MqDepartment> departments = this.baseMapper.selectList(wrapper);
        PageInfo<MqDepartment> pageInfo = new PageInfo<MqDepartment>(departments);
        return pageInfo;
    }


    @Override
    public List<MqDepartment> Tree(String parentId, String tenantId) {

        QueryWrapper<MqDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqDepartment::getParentId, parentId);
        queryWrapper.lambda().eq(MqDepartment::getTenantId, tenantId);
        return this.list(queryWrapper);
    }

    @Override
    public MqDepartmentVo Detail(String deptId) {

        MqDepartmentVo vo = new MqDepartmentVo();
        MqDepartment department = getById(deptId);

        BeanUtil.copyProperties(department, vo);

        if (department != null) {
            List<String> parentIds = new ArrayList<>();
            parentIds.add(department.getParentId());
            if (department.getParentId() != "0") {
                getParentIds(parentIds, department.getParentId());
            }

            vo.setParentIds(parentIds);
        }
        return vo;
    }

    private void getParentIds(List<String> parentIds, String deptId) {

        MqDepartment department = getById(deptId);
        if (department != null) {
            parentIds.add(0, department.getParentId());
            if (department.getParentId() != "0") {
                getParentIds(parentIds, department.getParentId());
            }
        }
    }
}
